<template>
	<view class="search" :class="theme+'-bg-middle'">
		<scroll-view scroll-y="true" style="height: 100%;">
			<uni-search-bar :placeholder="$t('Search.Entersearchcontent')" :cancelText="$t('Search.Cancel')"
				:isCilck="true" :focus="true" v-model="val" />
			<view class="nav" v-if="searchList.length && !searchDatalist.length">
				<view class="nav-l">
					<view class="icon">
						<image src="../../../static/images/icons/lishisousuo.png"></image>
					</view>
					<view class="text">
						<text :class="theme + '-ftext-color'">{{$t('Search.Historicalsearch')}}</text>
					</view>
				</view>
				<view class="nav-r" @click="clearSearchList">
					<view class="text">
						<text :class="theme + '-ftext-color'">{{$t('Search.Clear')}}</text>
					</view>
				</view>

			</view>

			<view class="toolLink" v-if="!searchDatalist.length">
				<text class="br-4" :class="[`${theme+'-bg-dbox'}`,`${theme}-ftext-color`]"
					v-for="(item,index) in searchList" :key="index"
					@click="go(item)">{{locale == 'en' ? item.english : item.title}}</text>
			</view>

			<view class="linkBox" v-if="searchDatalist.length && !isShow">
				<view class="link br-1" :class="theme+'-bg-bbox'" v-for="(item,index) in searchDatalist" :key="item.id"
					@click="go(item)" @longpress="isShow=true">
					<text :class="theme+'-ftext-color'">{{locale == 'en' ? item.english : item.title}}</text>
				</view>
			</view>

			<view class="linkB" v-if="isShow">
				<view class="link br-1" :class="theme+'-bg-bbox'" v-for="(item,index) in searchDatalist" :key="index"
					:style="item.isCollection ? 'opacity: 0.5;' : ''" @longpress="isShow=false"
					@click="item.isCollection ? null : check(index)">
					<text :class="theme+'-ftext-color'">{{locale == 'en' ? item.english : item.title}}</text>
					<checkbox :checked="item.isCheckd" :disabled="item.isCollection" color="#000000" />
				</view>
			</view>

			<view class="btn br-4" v-if="isShow" :class="theme+'-bg-abox'" @click="add">
				<uni-icons type="checkmarkempty" size="50" color="#ffffff"></uni-icons>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				toolsList: [],
				collectList: [],
				searchList: [],
				searchData: [],
				val: {
					value: ''
				},
				isShow: false,
			};
		},
		computed: {
			searchDatalist() {

				if (!this.val.value) {
					this.isShow = false
					this.getData()
					return []
				}

				if (this.locale == 'en') {
					return this.toolsList.filter(
						item =>
						item.english.includes(this.val.value.replace(/\s/g, "")) ||
						item.english.toUpperCase().includes(this.val.value.replace(/\s/g, "").toUpperCase()) ||
						item.english.toLowerCase().includes(this.val.value.replace(/\s/g, "").toLowerCase())
					)
				} else {
					return this.toolsList.filter(
						item =>
						item.title.includes(this.val.value.replace(/\s/g, "")) ||
						item.title.toUpperCase().includes(this.val.value.replace(/\s/g, "").toUpperCase()) ||
						item.title.toLowerCase().includes(this.val.value.replace(/\s/g, "").toLowerCase())
					)
				}


			}
		},
		onLoad() {
			this.$theme()
			this.getData()
		},
		methods: {
			go(item) {
				uni.getStorage({
					key: 'searchList',
					success: (res) => {
						res.data.forEach(ele => {
							if (ele.id != item.id) {
								res.data.unshift(item)
								uni.setStorage({
									key: 'searchList',
									data: res.data,
								})
							}
						})
					},
					fail: () => {
						let arr = []
						arr.unshift(item)
						uni.setStorage({
							key: 'searchList',
							data: arr,
						})
					}
				})

				this.getData()

				uni.navigateTo({
					url: item.url
				})
			},
			getData() {

				uni.getStorage({
					key: 'collectList',
					success: (res) => {
						this.collectList = res.data
					},
				})

				uni.getStorage({
					key: 'searchList',
					success: (res) => {
						this.searchList = res.data
					},
					fail: () => {
						this.searchList = []
					}
				})

				this.$get('/tool/list').then((res) => {
					res.data.forEach(item => item.isCheckd = false)
					res.data.forEach(item => item.isCollection = false)

					res.data.forEach(item => {
						this.collectList.forEach((ele) => {
							if (item.id == ele.id) {
								item.isCollection = true
							}
						})
					})

					this.toolsList = res.data

				})
			},
			clearSearchList() {
				uni.removeStorageSync('searchList')
				this.getData()
			},
			check(index) {
				this.searchDatalist[index].isCheckd = !this.searchDatalist[index].isCheckd
			},
			weChatLogin() {
				//#ifdef MP-WEIXIN
				uni.checkSession({
					success: (res) => {
						console.log(res)
					},
					fail: (err) => {
						console.log(err)
					}
				})
				uni.getUserProfile({
					desc: '登录',
					success: (res1) => {
						// console.log(res1.userInfo)
						uni.showLoading({
							title: '登录中...'
						})
						uni.getProvider({
							service: 'oauth',
							success: (res2) => {
								// console.log(res2)
								if (~res2.provider.indexOf('weixin')) {
									uni.login({
										provider: 'weixin',
										success: (res3) => {
											// console.log(res3)

											this.$post('/login/wx-mini', {
												code: res3.code
											}).then(res4 => {

												// console.log(res4.data)
												uni.hideLoading()
												uni.showToast({
													title: "登录成功",
													icon: 'none'
												});
												res4.data.nickname = res1
													.userInfo.nickName
												this.userInformation = res4
													.data
												uni.setStorage({
													key: 'userInformation',
													data: res4.data
												})
											})
										},
										fail: () => {
											uni.showToast({
												title: "微信登录授权失败",
												icon: "none"
											});
										}
									})

								} else {
									uni.showToast({
										title: '请先安装微信或升级版本',
										icon: "none"
									});
								}
							}
						});
					},
					fail: () => {
						// console.log(777)
						// uni.showToast({
						// 	title: "微信登录授权失败",
						// 	icon: "none"
						// });
					}
				})
				//#endif
			},
			add() {
				if (!uni.getStorageSync('userInformation').token) {
					// #ifdef APP-PLUS
					return uni.showToast({
						title: '需要登录才能收藏哦。',
						icon: 'none',
						success: () => {
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/mine/my-account/login/Login'
								})
							}, 500)
						}
					})
					// #endif
					// #ifdef MP-WEIXIN
					uni.showToast({
						title: '还未登录呢，请先登录',
						icon: 'none',
						success: () => {},
					})
					this.weChatLogin()
					return
					// #endif
				}

				let arr = []
				this.searchDatalist.forEach((item) => {
					if (item.isCheckd == true) {
						item.isCheckd = false
						item.isCollection = true
						this.collectList.unshift(item)
						arr.push(item.id)
					}
				})
				let str = arr.join(',')
				if (arr.length) {
					this.$post('/tool/collect', {
						toolId: str, //1,2 工具的id，多个工具以英文逗号分割
						type: 1, //1：收藏；2点赞
						action: 1 //0：取消收藏/点赞；1：收藏/点赞
					})

					this.toolsList.forEach((item, index) => {
						arr.forEach((ele) => {
							if (item.id == ele) {
								item.isCollection = true
							}
						})

					})

					uni.setStorage({
						key: 'collectList',
						data: this.collectList,
						success: () => {
							this.getData()
						}
					})

					uni.showToast({
						title: '添加成功。',
						icon: 'none'
					})
				} else {
					uni.showToast({
						title: '未添加喔。',
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.search {
		height: 100%;

		.nav {
			height: 95rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 48rpx 0 48rpx;
			cursor: pointer;

			.nav-l {
				display: flex;
				align-items: center;
			}

			.icon {
				width: 40rpx;
				height: 40rpx;
				margin-right: 30rpx;
			}

			.text {
				text {
					font-size: 26rpx;
					cursor: pointer;
				}
			}
		}

		.toolLink {
			padding: 0 0 0 36rpx;
			display: flex;
			flex-wrap: wrap;

			text /deep/ {
				display: block;
				padding: 20rpx 25rpx;
				// background: #F6F6F4;
				font-size: 24rpx;
				margin: 0 20rpx 25rpx 0;
				cursor: pointer;
			}

			// text:active {
			// 	background: $uni-bg-color-hover;
			// }
		}

		.linkBox {
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			justify-content: space-between;

			.link {
				width: 334rpx;
				height: 82rpx;
				font-size: 26rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				margin: 20rpx;
				cursor: pointer;


				text {
					cursor: pointer;
				}
			}
		}

		.linkB {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			align-items: center;

			.link {
				width: 334rpx;
				box-sizing: border-box;
				font-size: 26rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 18rpx 10rpx 18rpx 20rpx;
				margin: 20rpx;
				cursor: pointer;

				text {
					display: flex;
					justify-content: space-between;
					cursor: pointer;
				}
			}
		}

		.btn {
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
			// background-color: #0081ff;
			display: flex;
			justify-content: center;
			align-items: center;
			position: fixed;
			right: 40rpx;
			bottom: 60rpx;

			.uni-icons {
				font-size: 70rpx;
			}
		}
	}
</style>
